package com.wtgroup.demo.leetcode.q123_买卖股票的最佳时机III;

/**
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/
 *
 * @author 60906
 * @date 2021/5/8 15:39
 */
public class Q123_Test {

    /**
     * LC 动态规划
     *
     * 以更好理解的方式解读.
     * 思路: 买卖股票的最佳时机III.drawio
     */
    private class Solution {
        public int maxProfit(int[] prices) {
            int s1 = -prices[0], s12 = 0, s121 = -prices[0], s1212 = 0;
            for (int i = 1; i < prices.length; i++) {
                s1 = Math.max(s1, -prices[i]);
                s12 = Math.max(s12, s1 + prices[i]);
                s121 = Math.max(s121, s12 - prices[i]);
                s1212 = Math.max(s1212, s121 + prices[i]);
            }

            return Math.max(s12, s1212);
        }
    }

}
